package com.jvm123.api.api.controller

import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile

/**
 *
 * @author yawn
 *     2019/10/3 17:35
 *
 */
@RestController
@RequestMapping("base64")
class Base64Controller {

    @PostMapping("encode")
    String encode(@RequestParam(required = true) String s) {
        // BASE64 编码
        Base64.getEncoder().encodeToString(s.getBytes())
    }

    @PostMapping("decode")
    String decode(@RequestParam(required = true) String s) {
        // 还原BASE64
        new String(Base64.getDecoder().decode(s), "UTF-8")
    }

    @PostMapping("encode/pic")
    String encodePic(MultipartFile file) {
        long size = file.getSize()
        if (size > (1024 * 1000)) {
            throw new RuntimeException("文件太大")
        }
        Base64.getEncoder().encodeToString(file.getBytes())
    }

    @ExceptionHandler
    String exHandler(Exception e) {
        "转换出错：" + e.getMessage()
    }
}
